package com.ruoyi.boss.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.boss.domain.RegionCascader;
import com.ruoyi.boss.service.IRegionCascaderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 三级联动区域Controller
 * 
 * @author ruoyi
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/cascader")
public class RegionCascaderController extends BaseController
{
    @Autowired
    private IRegionCascaderService regionCascaderService;

    /**
     * 查询三级联动区域列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RegionCascader regionCascader)
    {
        startPage();
        List<RegionCascader> list = regionCascaderService.selectRegionCascaderList(regionCascader);
        return getDataTable(list);
    }

    /**
     * 导出三级联动区域列表
     */
    @Log(title = "三级联动区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegionCascader regionCascader)
    {
        List<RegionCascader> list = regionCascaderService.selectRegionCascaderList(regionCascader);
        ExcelUtil<RegionCascader> util = new ExcelUtil<RegionCascader>(RegionCascader.class);
        util.exportExcel(response, list, "三级联动区域数据");
    }

    /**
     * 获取三级联动区域详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(regionCascaderService.selectRegionCascaderById(id));
    }

    /**
     * 新增三级联动区域
     */
    @Log(title = "三级联动区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegionCascader regionCascader)
    {
        return toAjax(regionCascaderService.insertRegionCascader(regionCascader));
    }

    /**
     * 修改三级联动区域
     */
    @Log(title = "三级联动区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegionCascader regionCascader)
    {
        return toAjax(regionCascaderService.updateRegionCascader(regionCascader));
    }

    /**
     * 删除三级联动区域
     */
    @Log(title = "三级联动区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionCascaderService.deleteRegionCascaderByIds(ids));
    }
    
    /**
     * 根据父级ID查询子区域列表
     */
    @GetMapping("/listByParent/{parentId}")
    public AjaxResult listByParent(@PathVariable("parentId") Long parentId)
    {
        List<RegionCascader> list = regionCascaderService.selectRegionCascaderByParentId(parentId);
        // 转换为前端需要的格式
        List<RegionOption> options = list.stream().map(region -> {
            RegionOption option = new RegionOption();
            option.setValue(region.getId());
            if (region.getProvince() != null) {
                option.setLabel(region.getProvince());
            } else if (region.getCity() != null) {
                option.setLabel(region.getCity());
            } else if (region.getDistrict() != null) {
                option.setLabel(region.getDistrict());
            }
            return option;
        }).collect(Collectors.toList());
        return AjaxResult.success(options);
    }
    
    /**
     * 区域选项类（用于前端级联选择器）
     */
    public static class RegionOption {
        private Long value;
        private String label;
        
        public Long getValue() {
            return value;
        }
        
        public void setValue(Long value) {
            this.value = value;
        }
        
        public String getLabel() {
            return label;
        }
        
        public void setLabel(String label) {
            this.label = label;
        }
    }
}